package org.gruposp2p.proyectokarma.gwt.client.util;

import com.google.gwt.http.client.URL;

public class ServerPaths {

	public static final String COMETD_SERVICE = "http://localhost:8080/ProyectoKarma/cometd";
        
    public static String getLoginPath () {
        return "/ProyectoKarma/j_spring_security_check";
    } 
    
    public static String getLogoutPath () {
        return getApplicationPath() + "logout/index";
    } 
    
    public static String getEditorPath () {
        return getApplicationPath() + "editor/index";
    } 
    
    public static String getDocumentsPath () {
        return getApplicationPath() + "documents";
    } 
    
    public static String getRegisterPath () {
        return getApplicationPath() + "register/register";
    } 
    
    public static String getDocumentPath (String documentId) {
        return getApplicationPath() + "documents/" + documentId;
    } 
    
    public static String getDocumentPath (Long documentId) {
        return getDocumentPath (String.valueOf(documentId));
    } 
    
    public static String getDocumentsGetPath (int max, int offset) {
        return getApplicationPath() + "documents?max=" + max + "&offset=" + offset;
    }
    
    
    public static String getResetPasswordPath (String token) {
        return getApplicationPath() + "register/resetPassword?t=" + token;
    } 
    
    public static String getForgotPasswordPath () {
        return getApplicationPath() + "register/forgotPassword";
    } 
    
    public static String getAddressesPath () {
        return getApplicationPath() + "addresses";
    } 
    
    public static String getAddressPath (String addressId) {
        return getApplicationPath() + "addresses/" + addressId;
    } 
    public static String getVotesPath () {
        return getApplicationPath() + "votes";
    } 
    
    public static String getVotePath (String voteId) {
        return getApplicationPath() + "votes/" + voteId;
    } 
  
    public static String getTagsPath () {
        return getApplicationPath() + "tags/";
    } 
    
    public static String getDBConsolePath () {
        return getApplicationPath() + "console";
    } 
    
    public static String getDeleteTagLinkImagePath () {
        return getApplicationPath() + "images/xit.gif";
    }  
    
    public static String getSearchDocumentPath (String textQuery) {
        return getApplicationPath() + "search/document?textQuery=" + textQuery;
    } 
    
    public static String getSearchDocumentByTagPath (String tagName) {
        return getApplicationPath() + "search/documentByTag?tagName=" + tagName;
    }   
    
    public static String getSearchCommentPath (String textQuery) {
        return getApplicationPath() + "search/comment?textQuery=" + textQuery;
    } 
    
    public static String getSearchPeerPath (String textQuery) {
        return getApplicationPath() + "search/peer?textQuery=" + textQuery;
    } 
    
    public static String getQRCodeUrl(int sizeX, int sizeY, String content) {
        StringBuilder result = new StringBuilder();
        result.append("http://chart.apis.google.com/chart?cht=qr&chs=");
        result.append(sizeX).append('x').append(sizeY);
        result.append("&chl=");
        result.append(URL.encodeQueryString(content));
        return result.toString();
      }
    
    public static String getApplicationPath () {
    	String applicationPath = "http://" +
			com.google.gwt.user.client.Window.Location.getHostName() + ":" +
			com.google.gwt.user.client.Window.Location.getPort();
        String totalPath = com.google.gwt.user.client.Window.Location.getPath();
        int indexOfSecondSlash = totalPath.indexOf("/", 1);
        if (indexOfSecondSlash != -1) {
        	applicationPath = applicationPath + 
        		totalPath.substring(0, indexOfSecondSlash) + "/";
        }
        return applicationPath;
    }
    
    //redirect the browser to the given url
    public static native void redirect(String url)/*-{
          $wnd.location = url;
    }-*/;

    public static native void alert(String msg)/*-{
          $wnd.alert(msg);
    }-*/;
    
    public static native String openInNewTab(String url)/*-{
    	return $wnd.open(url, 'target=_blnk')
    }-*/;
}